/**
 * Created by Celebration-Grey on 2017/4/25.
 */

weibo.profile = {};

weibo.profile.init = function () {
    weibo.profile.topNav();
    weibo.profile.photo();
    if(localStorage.getItem('user_info') != null){
        userInfo = localStorage.getItem('user_info');
        userInfo = $.parseJSON(userInfo);
        var u_nickname = userInfo['user_nickname'];
        u_nickname = weibo.public.htmlEncodeByRegExp(u_nickname);
        $('.user-nickname').html(u_nickname);
        weibo.profile.getUserInfo();
    }
    else{
        alert('请先登录！');
        window.location.href = 'login.html';
    }
    // setInterval(function(){
    //     console.log(document.body.clientWidth);
    // },10)
};

weibo.profile.topNav = function () {
    weibo.public.hoverShow($('.gn_set_list .glyphicon-cog'),$('.gn_set_list .gn_topmenulist_set'));
};

weibo.profile.photo = function(){
    $('.pf_photo').hover(function(){
        $(this).addClass('pf_photo_active');
    },function(){
        $(this).removeClass('pf_photo_active');
    });
};

weibo.profile.menu = function (uid,unickname,relationShip) {
    var domString = '';
    var $dom = null;
    if(relationShip == 0 || relationShip == 2){
        domString = '<div class="pf_opt"><div class="opt_box clearfix"><div class="btn_bed W_fl follow-btn"><a href="javascript:void(0);"class="W_btn_c btn_34px"><em class="W_ficon ficon_add S_ficon">+</em>关注</a></div><div class="btn_bed W_fl"><a href="javascript:;"class="W_btn_d btn_34px">私信</a></div><div class="btn_bed W_fl"><a href="javascript:;"class="W_btn_d W_btn_pf_menu btn_34px"><em class="W_ficon ficon_menu S_ficon">=</em></a></div></div></div>';
        $dom = $(domString);
        $dom.find('.follow-btn').click(function () {
            $(this).addClass('W_btn_a_disable');
            weibo.public.followAction(uid,weibo.profile.followSuccess);
        });
    }
    else if(relationShip == 1){
        domString = '<div class="pf_opt"><div class="opt_box clearfix"><div class="btn_bed W_fl following-btn"><a href="javascript:void(0);"class="W_btn_d btn_34px"><em class="W_ficon ficon_right S_ficon">Y</em>已关注<em class="W_ficon ficon_arrow_down_lite S_ficon">g</em></a></div><div class="btn_bed W_fl"><a href="javascript:;"class="W_btn_d btn_34px">私信</a></div><div class="btn_bed W_fl"><a href="javascript:;"class="W_btn_d W_btn_pf_menu btn_34px"><em class="W_ficon ficon_menu S_ficon">=</em></a></div></div></div>';
        $dom = $(domString);
        var $menu = weibo.profile.layerMenu(uid,unickname);
        $('body').append($menu);
        var timer = setTimeout(function(){},10000);
        $dom.find('.following-btn').hover(function () {
             $menu.css('top',$(this).offset().top + 34).css('left',$(this).offset().left - 14);
             //$menu.unbind('mouseleave');
             clearTimeout(timer);
            $menu.show();
        },function () {
            timer = setTimeout(function(){
                $menu.hide();
            },500);
            $menu.mouseenter(function(){
                clearTimeout(timer);
            });
            $menu.mouseleave(function(){
                setTimeout(function(){
                    $menu.hide();
                },500);
            });
        })

    }
    else if(relationShip == 3){

    }
    $('.shadow.S_shadow').append($dom);
};

weibo.profile.layerMenu = function (uid,unickname) {
    var domString = '';
    domString = '<div class="layer_menu_list_b animated fadeInUp"style="position: absolute; z-index: 3000;min-width: 98px; display: none"id="layer_menu"><div class="list_wrap"><div class="list_content W_f14"><ul class="list_ul"><li class="item"><a href="javascript:void(0);"class="tlink S_txt1 friend-action">加为好友</a></li><li class="item"><a href="javascript:void(0);"class="tlink S_txt1 unfolloing-action">取消关注</a></li><li class="item"><a href="javascript:void(0);"class="tlink S_txt1">设置分组</a></li></ul></div></div></div>';
    var $dom = $(domString);
    $dom.find('.unfollowing-action').click(function () {
        $(this).addClass('btn_disable');
        weibo.public.unfollowAction(uid,weibo.profile.unfollowSuccess);
    });
    $dom.find('.friend-action').click(function () {
        $('body').append(weibo.profile.shadow()).append(weibo.friend.layer(uid,unickname));
    });
    return $dom;
};

weibo.profile.getUserInfo = function () {
    var uid = weibo.public.getQueryString('uid');
    $.ajax({
        type: 'GET',
        url: host + 'weibo_uidInfo',
        data: {
            uid: uid
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.status == 200) {
                var $data = data.data;
                weibo.profile.setUserInfo($data['info']);
                weibo.profile.getUserBowen();
                if(!$data['is_self']){
                    weibo.profile.menu($data['info']['user_id'],$data['info']['user_nickname'],$data['relationship'])
                }
                else{
                    weibo.profile.headChange();
                    weibo.profile.upCover();
                    weibo.profile.upBackground();
                }
            }
            else if (data.status == 503) {
                alert(data.message);
                window.location.href = 'home.html';
            }
            else if (data.status == 304) {
                alert(data.message);
            }
            else if (data.status == 500) {
                alert(data.message);
                window.location.href = 'home.html';
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.profile.upCover = function () {
    $('.PCD_header').addClass('self');
    $('.self .upcover a').click(function(){
        weibo.profile.coverSetBox();
    });
};

weibo.profile.upBackground = function(){
    $('.pf_wrap a.icon_setskin').click(function(){
        weibo.profile.bgSetBox();
    });
};

weibo.profile.bowenTotal = null;
weibo.profile.pageMaxSize = 100;
weibo.profile.start = 0;
weibo.profile.size = 10;
weibo.profile.uid = weibo.public.getQueryString('uid');

weibo.profile.setUserInfo = function (data) {
    var nickname = data['user_nickname'];
    var uid = data['user_id'];
    var bg = data['user_bg_img'];
    var card = data['user_card_img_big'];
    var head = data['user_head_img_big'];
    var info = data['user_info'];
    var followed = data['user_followed_count'];
    var following = data['user_following_count'];
    var bowen = data['user_bowen_count'];
    var sex = data['user_sex'];
    var level = data['user_level'];
    weibo.profile.bowenTotal = bowen;
    nickname = weibo.public.htmlEncodeByRegExp(nickname);
    info = weibo.public.htmlEncodeByRegExp(info);
    document.title = nickname+'的微博_微博';
    $('.pf_photo img').attr('src',head).attr('alt',nickname);
    $('h1.username').html(nickname);
    $('.pf_intro').html(info).attr('title',info);
    $('.cover_wrap').css('background-image','url('+card+')');
    $('.master-following-count').html(following);
    $('.master-bowen-count').html(bowen);
    $('.master-followed-count').html(followed);
    if (bg != null){
        $('body.FRAME_page').css('background-image','url('+bg+')');
    }
    var $sex = $('.pf_username a i');
    if (sex == 0){
        $sex.addClass('icon_pf_male');
    }
    else{
        $sex.addClass('icon_pf_female')
    }
};

weibo.profile.followSuccess = function (data) {
    var $btn = $('.follow-btn');
    if(data.status == 200){
        $btn.unbind('click');
        var a = '<a href="javascript:void(0);" class="W_btn_d btn_34px"><em class="W_ficon ficon_right S_ficon">Y</em>已关注<em class="W_ficon ficon_arrow_down_lite S_ficon">g</em></a>';
        $btn.removeClass('.follow-btn').removeClass('W_btn_a_disable').addClass('.following-btn');
        $btn.html(a);
    }
    else{
        $btn.removeClass('W_btn_a_disable');
    }
};

weibo.profile.unfollowSuccess = function (data) {
    var $btn = $('.following-btn');
    if(data.status == 200){
        var a = '<a href="javascript:void(0);" class="W_btn_c btn_34px"><em class="W_ficon ficon_add S_ficon">+</em>关注</a>';
        $btn.html(a);
        $btn.removeClass('.following-btn').addClass('.follow-btn');
    }
    else{
        $.Huimodalalert(data.message,2000);
    }
};

weibo.profile.getUserBowen = function () {
    var uid = weibo.profile.uid;
    var pagesize = weibo.profile.pageMaxSize;
    var size = 10;
    var total = weibo.profile.bowenTotal;
    var start = 0;
    var orderBy = 'time';
    var order = 'desc';
    weibo.profile.ajaxUserBowen(uid,orderBy,order,start,size,weibo.profile.bowenFnsuccess);
};

weibo.profile.ajaxUserBowen = function (uid,orderBy,order,start,size,fnSuccess) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_userBowen',
        data: {
            uid: uid,
            orderBy: orderBy,
            order: order,
            start: start,
            size: size
        },
        xhrFields: {
            withCredentials: true
        },
        success: fnSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.profile.bowenFnsuccess = function (data) {
    if(data.status == 200){
        var $data = data.data;
        var $dom = $('');
        for(var i = 0; i < $data.length; i++){
            var $bowen = weibo.bowen.bowenLi($data[i]['bowen'],$data[i]['author'],$data[i]['repost']);
            $("#loading").before($bowen);
        }
    }
    else{
        $.Huimodalalert(data.message,2000);
    }
};

weibo.profile.headSetBox = function(){
    var shadowString = '<div class="layer_shadow"style="position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0.3; z-index: 9999; background: rgb(0, 0, 0);"></div>';
    var setBoxString = '<div class="W_layer animated bounceIn"id="layer_head"style=""><div tabindex="0"></div><div class="content"><div class="W_layer_title"style="cursor: default;">头像设置</div><div class="W_layer_close"><a href="javascript:void(0);"class="W_ficon ficon_close S_ficon">X</a></div><div><div class="layer_edit_avatar"><div class="WB_minitab"><ul class="minitb_ul S_line1 clearfix"><li class="minitb_item S_line1 current"><a class="minitb_lk S_txt1 S_bg2"href="javascript:void(0);">本地上传</a><span class="cur_block"></span></li><li class="minitb_item S_line1 "><a class="minitb_lk S_txt1">微博相册</a><span class="cur_block"></span></li></ul></div><div class="edit_avatar clearfix"><div class="edit_left W_fl"><div class="initial_pic"><div class="loading_block"style="display:none;"><i class="W_loading_big"></i></div><div class="choose_pic W_tc"><a class="W_btn_b"href="javascript:void(0);"style="position: relative;overflow: hidden;"><em class="W_ficon ficon_add S_ficon">+</em>选择图片<form enctype="multipart/form-data"method="post"class="avatar_form"><input class="pic1"accept="image/gif, image/jpeg, image/x-png"type="file"name="avatar_file"style="position:absolute;bottom:0;right:0;filter:alpha(opacity=0);-moz-opacity:0;cursor:pointer;width:1000px;height:1000px;font-size:100px;"></form><form enctype="multipart/form-data"method="post"class="crop_form"><input type="hidden"name="avatar_data"/><input type="hidden"name="avatar_type"/><input type="hidden"name="avatar_src"/><input type="hidden"name="avatar_name"/></form></a><p class="S_txt2"style="display:block;">只支持JPG、PNG、GIF，大小不超过5M</p><a href="javascript:void(0);"class="W_btn_b go_back" style="display: none;"><span class="glyphicon glyphicon-share-alt"></span>返回</a><p class="tips S_txt2 error_show"style="display:none;"><span class="W_icon icon_rederrorS"></span><span class="error_info">上传失败！上传图片宽度不能小于100px</span></p></div><div class="edit_pic"style="display:none;"><div class="img_wrap W_tc"><span class="canvas_wrap"></span></div></div></div><div class="fun_area clearfix"style="display:none;"><a class="W_btn_b W_fl re_upload"href="javascript:void(0);"style="position: relative;overflow: hidden;margin-right: 23px;"><span>重新上传</span></a><a class="W_btn_a W_fl crop_left"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-left"></span></a><a class="W_btn_a W_fl crop_right"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-right"></span></a><a class="W_btn_a W_fl crop_up"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-up"></span></a><a class="W_btn_a W_fl crop_down"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-down"></span></a><a class="W_btn_a W_fl crop_zoom_in"href="javascript:void(0);"><span class="glyphicon glyphicon-zoom-in"></span></a><a class="W_btn_a W_fl crop_zoom_out"href="javascript:void(0);"><span class="glyphicon glyphicon-zoom-out"></span></a><a class="W_btn_b W_fr crop_rotate"href="javascript:void(0);"><em class="W_ficon ficon_turnleft">m</em><span>90°旋转</span></a></div></div><div class="edit_right W_fr"><div class="preview W_tc"><p class="tit">预览</p><div class="avatar avatar_100"><p class="pic_wrap"></p><span class="S_txt2">100px x 100px</span></div><div class="avatar avatar_50"><p class="pic_wrap"></p><span class="S_txt2">50px x 50px</span></div><div class="avatar avatar_30"><p class="pic_wrap"></p><span class="S_txt2">30px x 30px</span></div></div></div></div></div><div class="W_layer_btn S_bg1"><a class="W_btn_a btn_34px upload-btn"href="javascript:void(0);">确定</a><a class="W_btn_b btn_34px btn_close"href="javascript:void(0);">取消</a></div></div></div></div>';
    var $shadow = $(shadowString);
    var $box = $(setBoxString);
    $box.css('top','20%').css('left',document.body.clientWidth / 2 - 270);
    $box.find('.ficon_close, .btn_close').click(function(){
        $('#image').cropper('destroy');
        $(this).parents('.W_layer').remove();
        $('.layer_shadow').remove();
    });
    $box.find('.go_back').click(function(){
        $('.choose_pic').hide();
        $('.edit_pic').show();
        $('.fun_area').show();
    });
    $box.find('input[name=avatar_file]').change(function () {
        $('.loading_block').show();
        $('.W_layer .go_back').show();
        weibo.profile.imgChange(1,'weibo_uploadHeadOriginal');
    });
    $box.find('.upload-btn').click(function(){
        $(this).addClass('W_btn_a_disable');
        $(this).html('上传中...');
        weibo.profile.uploadHeadImg();
    });
    weibo.profile.moveZoom($box);
    var $func = $box.find('.fun_area');
    var $reUpload = $func.find('.re_upload');
    $reUpload.click(function(){
        $('.choose_pic').show();
        $('.edit_pic').hide();
        $('.fun_area').hide();
    });
    $('body').append($shadow).append($box);
};

weibo.profile.headChange = function () {
    var $head = $('.pf_photo img');
    $head.click(function () {
        weibo.profile.headSetBox();
    });
};

weibo.profile.imgChange = function (size,url) {
    if( typeof(FileReader) === 'undefined'){
        alert('抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！');
    }
    else{
        var $avatarForm = $('.avatar_form');
        var $avatarFile = $avatarForm.find('input[name=avatar_file]');
        var file = $avatarFile[0].files[0];
        if(!/image\/\w+/.test(file.type)){
            alert('请确保文件为图像类型');
            return false;
        }
        else{
            var data = new FormData($avatarForm[0]);
            $.ajax({
                url : host + url,
                type : 'POST',
                dataType : 'json',
                data: data,
                processData: false,
                contentType: false,
                xhrFields: {
                    withCredentials: true
                },
                success: function(data){
                    if(data.status == 200){
                        weibo.profile.drawImage(size,data.data['url'],data.data['src'],data.data['name'],data.data['type']);
                    }   
                    else{
                        $.Huimodalalert(data.message,2000);
                        weibo.profile.uploadFailed(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
    }
};

weibo.profile.drawImage = function($size,url,src,name,type){
    $('#image').cropper('destroy');
    $('.error_show').hide();
    var $wrap = $('.canvas_wrap');
    $wrap.children().remove();
    var $cropForm = $('.crop_form');
    $cropForm.find('input[name=avatar_name]').val(name);
    $cropForm.find('input[name=avatar_src]').val(src);
    $cropForm.find('input[name=avatar_type]').val(type);
    var $img = new Image();
    $img.src = url;
    $($img).attr('id','image');
    $img.onload = function(){
        var size = this.width / this.height;
        var width = size *360;
        this.width = width;
        this.height = 360;
    };
    $wrap.append($img);
    weibo.profile.cropImage($size,$('#image'),$cropForm);
};

weibo.profile.cropImage = function($size,$image,$form){
    $avatarData = $form.find('input[name=avatar_data]');
    $image.cropper({
        aspectRatio: $size,
        preview: '.pic_wrap',
        crop : function(e){
            var json = [
                  '{"x":' + e.x,
                  '"y":' + e.y,
                  '"height":' + e.height,
                  '"width":' + e.width,
                  '"rotate":' + e.rotate + '}'
                ];
            $avatarData.val(json);
        },
        ready : function () {
            $('.loading_block').hide();
            $('.choose_pic').hide();
            $('.edit_pic').show();
            $('.fun_area').show();
        }
    });
};

weibo.profile.uploadFailed = function(msg){
    $('.loading_block').hide();
    $('.choose_pic').show();
    $('.error_show').show();
    $('.error_info').html(msg);
};

weibo.profile.uploadHeadImg = function(){
    var $cropForm = $('.crop_form');
    var data = new FormData($cropForm[0]);
    $.ajax({
        url : host + 'weibo_uploadHeadImg',
        type : 'POST',
        dataType : 'json',
        data: data,
        processData: false,
        contentType: false,
        xhrFields: {
            withCredentials: true
        },
        success: function(data){
            if (data.status == 200) {
                $('#image').cropper('destroy');
                $('.W_layer').remove();
                $('.layer_shadow').remove();
                $('.photo_wrap img.photo').attr('src',data.data['big']);
                $('.bowen-user-head img').attr('src',data.data['middle']);
            }
            else {
                $.Huimodalalert(data.message,2000);
                weibo.profile.uploadFailed(data.message);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.profile.coverSetBox = function(){
    var shadowString = '<div class="layer_shadow"style="position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0.3; z-index: 9999; background: rgb(0, 0, 0);"></div>';
    var setBoxString = '<div class="W_layer animated bounceIn"id="layer_cover"style=""><div tabindex="0"></div><div class="content"><div class="W_layer_title"style="cursor: default;">封面设置</div><div class="W_layer_close"><a href="javascript:void(0);"class="W_ficon ficon_close S_ficon">X</a></div><div><div class="layer_edit_avatar"><div class="WB_minitab"><ul class="minitb_ul S_line1 clearfix"><li class="minitb_item S_line1 current"><a class="minitb_lk S_txt1 S_bg2"href="javascript:void(0);">本地上传</a><span class="cur_block"></span></li><li class="minitb_item S_line1 "><a class="minitb_lk S_txt1">微博相册</a><span class="cur_block"></span></li></ul></div><div class="edit_avatar clearfix"><div class="edit_left W_fl"><div class="initial_pic"><div class="loading_block"style="display:none;"><i class="W_loading_big"></i></div><div class="choose_pic W_tc"><a class="W_btn_b"href="javascript:void(0);"style="position: relative;overflow: hidden;"><em class="W_ficon ficon_add S_ficon">+</em>选择图片<form enctype="multipart/form-data"method="post"class="avatar_form"><input class="pic1"accept="image/gif, image/jpeg, image/x-png"type="file"name="avatar_file"style="position:absolute;bottom:0;right:0;filter:alpha(opacity=0);-moz-opacity:0;cursor:pointer;width:1000px;height:1000px;font-size:100px;"></form><form enctype="multipart/form-data"method="post"class="crop_form"><input type="hidden"name="avatar_data"/><input type="hidden"name="avatar_type"/><input type="hidden"name="avatar_src"/><input type="hidden"name="avatar_name"/></form></a><p class="S_txt2"style="display:block;">只支持JPG、PNG、GIF，大小不超过5M</p><a href="javascript:void(0);"class="W_btn_b go_back"style="display: none;"><span class="glyphicon glyphicon-share-alt"></span>返回</a><p class="tips S_txt2 error_show"style="display:none;"><span class="W_icon icon_rederrorS"></span><span class="error_info">上传失败！上传图片宽度不能小于100px</span></p></div><div class="edit_pic"style="display:none;"><div class="img_wrap W_tc"><span class="canvas_wrap"></span></div></div></div><div class="fun_area clearfix"style="display:none;"><a class="W_btn_b W_fl re_upload"href="javascript:void(0);"style="position: relative;overflow: hidden;margin-right: 73px;"><span>重新上传</span></a><a class="W_btn_a W_fl crop_left"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-left"></span></a><a class="W_btn_a W_fl crop_right"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-right"></span></a><a class="W_btn_a W_fl crop_up"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-up"></span></a><a class="W_btn_a W_fl crop_down"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-down"></span></a><a class="W_btn_a W_fl crop_zoom_in"href="javascript:void(0);"><span class="glyphicon glyphicon-zoom-in"></span></a><a class="W_btn_a W_fl crop_zoom_out"href="javascript:void(0);"><span class="glyphicon glyphicon-zoom-out"></span></a><a class="W_btn_b W_fr crop_rotate"href="javascript:void(0);"><em class="W_ficon ficon_turnleft">m</em><span>90°旋转</span></a></div></div><div class="edit_right W_fr"><div class="preview W_tc"><p class="tit">预览</p><div class="avatar"><p class="pic_wrap" style="width:306px;height:100px"></p><span class="S_txt2">306px x 100px</span></div><div class="avatar"><p class="pic_wrap" style="width:100px;height:33px"></p><span class="S_txt2">100px x 33px</span></div></div></div></div></div><div class="W_layer_btn S_bg1"><a class="W_btn_a btn_34px upload-btn"href="javascript:void(0);">确定</a><a class="W_btn_b btn_34px btn_close"href="javascript:void(0);">取消</a></div></div></div></div>';
    var $shadow = $(shadowString);
    var $box = $(setBoxString);
    $box.css('width',850);
    $box.css('top','20%').css('left',document.body.clientWidth / 2 - 425);
    $box.find('.ficon_close, .btn_close').click(function(){
        $('#image').cropper('destroy');
        $(this).parents('.W_layer').remove();
        $('.layer_shadow').remove();
    });
    $box.find('.go_back').click(function(){
        $('.choose_pic').hide();
        $('.edit_pic').show();
        $('.fun_area').show();
    });
    $box.find('input[name=avatar_file]').change(function () {
        $('.loading_block').show();
        $('.W_layer .go_back').show();
        weibo.profile.imgChange(3.06,'weibo_uploadCoverOriginal');
    });
    $box.find('.upload-btn').click(function(){
        $(this).addClass('W_btn_a_disable');
        $(this).html('上传中...');
        weibo.profile.uploadCoverImg();
    });
    weibo.profile.moveZoom($box);
    var $func = $box.find('.fun_area');
    var $reUpload = $func.find('.re_upload');
    $reUpload.click(function(){
        $('.choose_pic').show();
        $('.edit_pic').hide();
        $('.fun_area').hide();
    });
    $('body').append($shadow).append($box);      
};

weibo.profile.uploadCoverImg = function(){
    var $cropForm = $('.crop_form');
    var data = new FormData($cropForm[0]);
    $.ajax({
        url : host + 'weibo_uploadCoverImg',
        type : 'POST',
        dataType : 'json',
        data: data,
        processData: false,
        contentType: false,
        xhrFields: {
            withCredentials: true
        },
        success: function(data){
            if (data.status == 200) {
                $('#image').cropper('destroy');
                $('.W_layer').remove();
                $('.layer_shadow').remove();
                $('.cover_wrap').css('background-image','url('+data.data['big']+')');
            }
            else {
                $.Huimodalalert(data.message,2000);
                weibo.profile.uploadFailed(data.message);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.profile.moveZoom = function($box){
    $box.find('.crop_left').click(function(){
        $('#image').cropper('move',-10,0);
    });
    $box.find('.crop_right').click(function(){
        $('#image').cropper('move',10,0);
    });
    $box.find('.crop_up').click(function(){
        $('#image').cropper('move',0,-10);
    });
    $box.find('.crop_down').click(function(){
        $('#image').cropper('move',0,10);
    });
    $box.find('.crop_rotate').click(function(){
        $('#image').cropper('rotate',-90);
    });
    $box.find('.crop_zoom_in').click(function(){
        $('#image').cropper('zoom',0.1);
    });
    $box.find('.crop_zoom_out').click(function(){
        $('#image').cropper('zoom',-0.1);
    });   
};

weibo.profile.shadow = function () {
    var $shadow = '<div class="layer_shadow"style="position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0.3; z-index: 9999; background: rgb(0, 0, 0);"></div>';
    $shadow = $($shadow);
    return $shadow;
    //$('body').append($shadow);
};

weibo.profile.bgSetBox = function(){
    var shadowString = '<div class="layer_shadow"style="position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0.3; z-index: 9999; background: rgb(0, 0, 0);"></div>';
    var setBoxString = '<div class="W_layer animated bounceIn"id="layer_bg"style=""><div tabindex="0"></div><div class="content"><div class="W_layer_title"style="cursor: default;">封面设置</div><div class="W_layer_close"><a href="javascript:void(0);"class="W_ficon ficon_close S_ficon">X</a></div><div><div class="layer_edit_avatar"><div class="WB_minitab"><ul class="minitb_ul S_line1 clearfix"><li class="minitb_item S_line1 current"><a class="minitb_lk S_txt1 S_bg2"href="javascript:void(0);">本地上传</a><span class="cur_block"></span></li><li class="minitb_item S_line1 "><a class="minitb_lk S_txt1">微博相册</a><span class="cur_block"></span></li></ul></div><div class="edit_avatar clearfix"><div class="edit_left W_fl"><div class="initial_pic"><div class="loading_block"style="display:none;"><i class="W_loading_big"></i></div><div class="choose_pic W_tc"><a class="W_btn_b"href="javascript:void(0);"style="position: relative;overflow: hidden;"><em class="W_ficon ficon_add S_ficon">+</em>选择图片<form enctype="multipart/form-data"method="post"class="avatar_form"><input class="pic1"accept="image/gif, image/jpeg, image/x-png"type="file"name="avatar_file"style="position:absolute;bottom:0;right:0;filter:alpha(opacity=0);-moz-opacity:0;cursor:pointer;width:1000px;height:1000px;font-size:100px;"></form><form enctype="multipart/form-data"method="post"class="crop_form"><input type="hidden"name="avatar_data"/><input type="hidden"name="avatar_type"/><input type="hidden"name="avatar_src"/><input type="hidden"name="avatar_name"/></form></a><p class="S_txt2"style="display:block;">只支持JPG、PNG、GIF，大小不超过5M</p><a href="javascript:void(0);"class="W_btn_b go_back"style="display: none;"><span class="glyphicon glyphicon-share-alt"></span>返回</a><p class="tips S_txt2 error_show"style="display:none;"><span class="W_icon icon_rederrorS"></span><span class="error_info">上传失败！上传图片宽度不能小于100px</span></p></div><div class="edit_pic"style="display:none;"><div class="img_wrap W_tc"><span class="canvas_wrap"></span></div></div></div><div class="fun_area clearfix"style="display:none;"><a class="W_btn_b W_fl re_upload"href="javascript:void(0);"style="position: relative;overflow: hidden;margin-right: 73px;"><span>重新上传</span></a><a class="W_btn_a W_fl crop_left"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-left"></span></a><a class="W_btn_a W_fl crop_right"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-right"></span></a><a class="W_btn_a W_fl crop_up"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-up"></span></a><a class="W_btn_a W_fl crop_down"href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-down"></span></a><a class="W_btn_a W_fl crop_zoom_in"href="javascript:void(0);"><span class="glyphicon glyphicon-zoom-in"></span></a><a class="W_btn_a W_fl crop_zoom_out"href="javascript:void(0);"><span class="glyphicon glyphicon-zoom-out"></span></a><a class="W_btn_b W_fr crop_rotate"href="javascript:void(0);"><em class="W_ficon ficon_turnleft">m</em><span>90°旋转</span></a></div></div><div class="edit_right W_fr"><div class="preview W_tc"><p class="tit">预览</p><div class="avatar"><p class="pic_wrap" style="width:320px;height:180px"></p><span class="S_txt2">320px x 180px</span></div></div></div></div></div><div class="W_layer_btn S_bg1"><a class="W_btn_a btn_34px upload-btn"href="javascript:void(0);">确定</a><a class="W_btn_b btn_34px btn_close"href="javascript:void(0);">取消</a></div></div></div></div>';
    var $shadow = $(shadowString);
    var $box = $(setBoxString);
    $box.css('width',850);
    $box.css('top','20%').css('left',document.body.clientWidth / 2 - 425);
    $box.find('.ficon_close, .btn_close').click(function(){
        $('#image').cropper('destroy');
        $(this).parents('.W_layer').remove();
        $('.layer_shadow').remove();
    });
    $box.find('.go_back').click(function(){
        $('.choose_pic').hide();
        $('.edit_pic').show();
        $('.fun_area').show();
    });
    $box.find('input[name=avatar_file]').change(function () {
        $('.loading_block').show();
        $('.W_layer .go_back').show();
        weibo.profile.imgChange(1.8,'weibo_uploadBackgroundOriginal');
    });
    $box.find('.upload-btn').click(function(){
        $(this).addClass('W_btn_a_disable');
        $(this).html('上传中...');
        weibo.profile.uploadBackgroundImg();
    });
    weibo.profile.moveZoom($box);
    var $func = $box.find('.fun_area');
    var $reUpload = $func.find('.re_upload');
    $reUpload.click(function(){
        $('.choose_pic').show();
        $('.edit_pic').hide();
        $('.fun_area').hide();
    });
    $('body').append($shadow).append($box);
};

weibo.profile.uploadBackgroundImg = function(){
    var $cropForm = $('.crop_form');
    var data = new FormData($cropForm[0]);
    $.ajax({
        url : host + 'weibo_uploadBackgroundImg',
        type : 'POST',
        dataType : 'json',
        data: data,
        processData: false,
        contentType: false,
        xhrFields: {
            withCredentials: true
        },
        success: function(data){
            if (data.status == 200) {
                $('#image').cropper('destroy');
                $('.W_layer').remove();
                $('.layer_shadow').remove();
                $('body.S_page').css('background-image','url('+data.data['big']+')');
            }
            else {
                $.Huimodalalert(data.message,2000);
                weibo.profile.uploadFailed(data.message);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
}